#include"string.h"

namespace cls
{
	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* temp = new char[n+1];
			strcpy(temp,_str);
			_capacity = n;
			delete[] _str;
			_str = temp;
		}
	}

	void  string::push_back(char ch)
	{
		if (_size == _capacity)
		{
			size_t newcapacity = 0 == _capacity ? 4 : 2 * _capacity;
			reserve(newcapacity);
		}
		_str[_size] = ch;
		_str[_size+1] = '\0';
		++_size;
	}

	void string::append(const char* s)
	{
		size_t len = strlen(s);
		if (_size + len > _capacity)
		{
			reserve(_size+len > _capacity*2 ? _size + len : 2*_capacity);
		}
		strcpy(_str+_size,s);
		_size += len;
	}

	string& string::operator+=(char ch)
	{
		push_back(ch);
		return *this;
	}

	string& string::operator+=(const  char* s)
	{
		append(s);
		return *this;
	}

	void string::insert(size_t pos, char ch)
	{
		assert(pos < _size);
		if (_size == _capacity)
		{
			reserve(2*_capacity);
		}
		size_t end = _size + 1;
		while (end > pos)
		{
			_str[end] = _str[end - 1];
			--end;
		}
		_str[pos] = ch;
		++_size;
	}

	void string::insert(size_t pos, const char* s)
	{
		assert(pos < _size);
		size_t len = strlen(s);
		if (_size+len > _capacity)
		{
			reserve(_size+len > 2*_capacity ? _size+len : 2*_capacity);
		}
		size_t end = _size + len;
		while (end > pos +len -1)
		{
			_str[end] = _str[end - len];
			--end;
		}
		size_t i = 0;
		for (i = 0;i < len;++i)
		{
			_str[pos+i] = s[i];
		}
		_size += len;
	}

	void string::erase(size_t pos, size_t n)
	{
		assert(pos <_size);
		if (_size-pos < n)
		{
			n = _size - pos;
		}
		size_t begin = pos + n;
		while (begin <= _size)
		{
			_str[begin - n] = _str[begin];
			++begin;
		}
		_size -= n;
	}

	size_t string::find(char ch, size_t pos)
	{
		assert(pos < _size);
		size_t i = 0;
		for (i = 0; i < _size; ++i)
		{
			if (ch == _str[i])
			{
				return i;
			}
		}
		return npos;
	}

	size_t  string::find(const char* s, size_t pos)
	{
		assert(pos < _size);
		char* ret = strstr(_str+pos,s);
		if (nullptr == ret)
		{
			return npos;
		}
		else
		{
			return ret - _str;
		}
	}

	string string::substr(size_t pos, size_t n)
	{
		assert(pos < _size);
		if (_size - pos < n)
		{
			n = _size - pos;
		}
		string temp;
		reserve(n);
		size_t i = pos;
		for (i = pos; i < pos + n; ++i)
		{
			temp += _str[i];
		}
		return temp;
	}

	bool string::operator<(const string& str)
	{
		return (strcmp(_str,str.c_str()) < 0);
	}

	bool string::operator<=(const string& str)
	{
		return *this < str || *this == str;
	}

	bool string::operator>(const string& str)
	{
		return !(*this <= str);
	}

	bool string::operator>=(const string& str)
	{
		return !(*this < str);
	}

	bool string::operator==(const string& str)
	{
		return strcmp(_str,str.c_str()) == 0;
	}

	bool string::operator!=(const string& str)
	{
		return !(*this == str);
	}

	const size_t string::npos = -1;

	ostream& operator<<(ostream& out, const string& str)
	{
		for (auto ch : str)
		{
			out << ch;
		}
		return out;
	}

	istream& operator>>(istream& in, string& str)
	{
		str.clear();
		const int N = 256;
		char buff[N] = {0};
		char ch = in.get();
		int i = 0;
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			ch = in.get();
			if (N-1 == i)
			{
				buff[i] = '\0';
				str += buff;
				i = 0;
			}
		}
		buff[i] = '\0';
		str += buff;

		return in;
	}

}